package com.example.flowabledemo.demos.flow.component;

import com.example.flowabledemo.demos.flow.entity.ProcessForm;
import com.example.flowabledemo.demos.flow.entity.ProcessFormDetail;
import com.example.flowabledemo.demos.flow.mapper.ProcessFormDetailMapper;
import com.example.flowabledemo.demos.flow.mapper.ProcessFormMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @Description: 表单组件
 * @Author: LuanKay
 * @Date: 2025/2/13
 */
@Slf4j
@Component
@AllArgsConstructor
public class FormComponent {
    private final ProcessFormMapper formMapper;

    private final ProcessFormDetailMapper formDetailMapper;

    /**
     * 新增表单
     *
     * @param form 表单实体
     * @return 表单id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long addForm(ProcessForm form) {
        // 表单表
        formMapper.insert(form);

        // 表单详情表
        Set<ProcessFormDetail> detailSet = form.getSets();
        if (!detailSet.isEmpty()) {
            detailSet.forEach(e -> e.setFormId(form.getId()));
            formDetailMapper.insertBatch(detailSet);
        }
        return form.getId();
    }
}
